package com.artron.mediaartron.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.ViewGroup;

import com.artron.baselib.utils.DensityUitls;


public class UserDefineImageView extends android.support.v7.widget.AppCompatImageView {

    private static final String TAG = "UserDefineImageView";
    private int mWidth;

    public UserDefineImageView(Context context) {
        super(context);
    }

    public UserDefineImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UserDefineImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int dip15 = DensityUitls.dip2px(15);
        mWidth = (metrics.widthPixels - dip15) / 3 - dip15;
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
//        Log.d(TAG, "width: " + bm.getWidth() + "\t height" + bm.getHeight());
        float height = (float)bm.getHeight() / ((float)bm.getWidth() / (float)mWidth);
        layoutParams.height = (int) height;
        this.setLayoutParams(layoutParams);
    }
}
